package com.cn.controller.dz;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cn.add.entity.Odercar_entity;
import com.cn.controller.BaseServlet;
import com.cn.service.Dz_chexing_contlr;

@WebServlet("/dz")
public class Dz extends BaseServlet {
	private Dz_chexing_contlr chexin;
	private int days ;
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm");
		Date date = new Date();
		Calendar cd=Calendar.getInstance();
		cd.setTime(date);
		cd.add(Calendar.DAY_OF_MONTH,2);
		req.setAttribute("datenow", sf.format(date));
		req.setAttribute("dateto", sf.format(cd.getTime()));
		chexin = new Dz_chexing_contlr();
		List<Odercar_entity> list = chexin.getcar(null);
		req.setAttribute("list", list);
		System.out.println(req.getSession().getAttribute("dayss"));
		forward("dz_/dz");
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String type[] = { "", "手动紧凑型", "经济型", "商务型", "豪华型", "SUV", "6至15座商务", "个性车", "电动车", "", "150", "300", "500",
				"10000", "", "奥迪", "宝马", "奔驰", "本田", "标致", "别克", "比亚迪", "长城", "大众", "丰田", "福特", "金杯", "凯迪拉克", "铃木",
				"马自达", "奇瑞", "	起亚", "斯柯达", "现代", "雪弗兰", "雪铁龙", "猎豹", "日产", "沃尔沃", "依维柯", "华泰", "野马", "三菱", "MINI",
				"雪铁龙DS", "北京汽车", "菲亚特", "江淮", "江铃", "上汽大通", "华颂", "风行", "广汽" };
		String cartypes = req.getParameter("ids");// 获取租车信息
		String day = req.getParameter("days");
		 days = Integer.parseInt(day.trim());// 获取租车天数
		String[] cartype = cartypes.split(",");
		String sql = "";
		String sqlt = "";
		String sqlc = "";
		String sqlb = "";
		for (int i = 0; i < cartype.length; i++) {
			int list = 0;
			if (cartype[i].toString() != null && cartype[i].toString() != "") {
				list = Integer.parseInt(cartype[i].toString());
			}
			if (list <= 8 && list != 0) {
				sqlt += "or c.car_type=" + "'" + type[list] + "'" + "  ";
			} else if (9 < list && list <= 13) {
				sqlc += "or a.lease_expense<" + type[list] + "  ";
			} else if (list > 14) {
				sqlb += "or b.brand=" + "'" + type[list] + "'" + "  ";
			}
		}

		if (sqlt != "") {
			sqlt = sqlt.substring(2, sqlt.length());
			sql += " AND " + sqlt;
		}
		if (sqlc != "") {
			sqlc = sqlc.substring(2, sqlc.length());
			sql += " AND " + sqlc;
		}
		if (sqlb != "") {
			sqlb = sqlb.substring(2, sqlb.length());
			sql += " AND " + sqlb;
		}
		List<Odercar_entity> list_change = chexin.getcar(sql);
		if (list_change.size()!=0) {
		 resp.getWriter().println( contrl(list_change));
		} else {
		 resp.getWriter().println(-1);
		}
		//System.out.println("查询记录+" + list_change.size());
	}

/**
 * 标签处理
 * @param list
 * @return
 */
public String  contrl(List<Odercar_entity> list){
//	String html = "<c:forEach items='${"+list+" }' var='a'>"
//			+ " <tr>"
//			+ " <td class='car_pic'><img src='${a.getAddress() }' /></td>"
//			+ " <td class='car_info'>"
//			+ " <div>"
//			+ " <p class='car_name'>${a.getBrand() }</p>"
//			+ " <p class='car_oth'>"
//			+ " ${a.getLablexiang() } | ${a.getLablequandon() } |"
//			+ " 乘坐${a.getSeating() }人 <input type='hidden' name='sum'"
//			+ " value='${a.getAddress() }'>"
//			+ " </p>"
//			+ " <c:choose>"
//			+ " <c:when test='${a.getHot()==1 }'>"
//			+ " <i class='car_hot'>热门车</i>"
//			+ " </c:when>"
//			+ " <c:when test='${a.getNew_()==1 }'>"
//			+ " <i class='car_new'>新车型</i>"
//			+ " </c:when>"
//		    + " <c:when test='${a.  getTianchuang()==1 }'>"
//			+ " <i class='car_new'>天窗车</i>"
//			+ " </c:when>"
//			+ " </c:choose>"
//			+ " </div>"
//			+ " </td>"
//			+ " <td class='car_odr'>"
//			+ " <div>"
//			+ " <div class='car_price'>"
//			+ " <span class='car_rmb'>￥</span><span name='rmb_cnt'"
//			+ " class='rmb_cnt'>${a.getLease_expense() }</span><span"
//			+ " class='odr_font'>/日均</span><br /> <span class='odr_font'>总价：￥</span><span"
//			+ " class='odr_font' name='money'>"
//			+ " ${a.getLease_expense()*2  } </span>"
//			+ " </div>"
//			+ " <div class='zc_btn' onclick='fun()'>租车</div>"
//			+ " </div>"
//			+ " </td>"
//			+ " </tr>"
//			+ " </c:forEach>";
	
		String html ="";
		for (Odercar_entity e : list) {
			int cost = e.getLease_expense()*days;
			html+=	" <tr>"	
					+ " <td class='car_pic'>"
					+ " <form action='car_info' method='post'><input name= 'carid'  type='hidden'  value='"+e.getId()+"'/></form>"
					+ " <img src="+e.getAddress()+"  onclick='car_info(this)' /></td>"
					+ " <td class='car_info'>"
					+ " <div>"
					+ " <p class='car_name'>"+e.getBrand()+"</p>"
					+ " <p class='car_oth'>"
					+ " "+e.getLablexiang()+" | "+e.getLablequandon()+" |"
					+ " 乘坐"+e.getSeating()+"人 <input type='hidden' name='sum'"
					+ " value="+e.getAddress()+">"
					+ " </p>";
			if(e.getHot()==1){
			html+=	 " <i class='car_hot'>热门车</i>"	;
			}
			if(e.getNew_()==1){
			html+=  " <i class='car_new'>新车型</i>"	;
			}
			if(e.getTianchuang()==1){
			html+=	 " <i class='car_new'>天窗车</i>";
			}
			html+=	 " </div>"
					+ " </td>"
					+ " <td class='car_odr'>"
					+ " <div>"
					+ " <div class='car_price'>"
					+ " <span class='car_rmb'>￥</span><span name='rmb_cnt'"
					+ " class='rmb_cnt'>"+e.getLease_expense()+"</span><span"
					+ " class='odr_font'>/日均</span><br /> <span class='odr_font'>总价：￥</span><span"
					+ " class='odr_font' name='money'>"
					+ ""+cost+" </span>"
					+ " </div>"
					+ " <div class='zc_btn' onclick='fun(this)'>"
					+ "租车</div>"
					+ " </div>"
					+ " </td>"
					+ " </tr>";
	
		}
	return html;
	}

}